// leanModal v1.1 by Ray Stone - http://finelysliced.com.au
// Dual licensed under the MIT and GPL

(function($) {
    $.fn.extend({
        leanModal: function(options) {
            var defaults = {
                overlay: 0.5,
                closeButton: '.x-btn'
            };
            !function () {
                if( !$('#lean_overlay').length > 0 ){
                    var overlay = $("<div id='lean_overlay'></div>");
                    $("body").append(overlay);
                }
                var css = '#lean_overlay {position: fixed; z-index: 100; top: 0px; left: 0px; height: 100%; width: 100%; background: #000; display: none; }',
                head = document.getElementsByTagName('head')[0],
                style = document.createElement('style');
                style.type = 'text/css';
                if (style.styleSheet) {
                    style.styleSheet.cssText = css;
                } else {
                    style.appendChild(document.createTextNode(css));
                }
                head.appendChild(style);
            }();
            options = $.extend(defaults, options);

            (function(){
                $("a[href^='#']").addClass('pop-cbtn');
            })();
            return this.each(function() {
                var o = options;
                var _this = $(this);
                $(document).on('click','.pop-cbtn', function(e){

                    var modal_id = $(_this).attr("href");
                    console.log(_this);

                    if( !$(modal_id).length > 0 ) return;
                   
                    var modal_height = $(modal_id).outerHeight();
                    var modal_width = $(modal_id).outerWidth();
                    $("#lean_overlay").css({
                        "display": "block",
                        opacity: 0
                    });
                    $("#lean_overlay").fadeTo(200, o.overlay);
                    $(modal_id).css({
                        "display": "block",
                        "position": "fixed",
                        "opacity": 0,
                        "z-index": 11000,
                        "left": 50 + "%",
                        "top": 50 + '%',
                        "margin-left": -(modal_width / 2) + "px",
                        "margin-top": -(modal_height / 2) + "px",
                    });
                    $(modal_id).fadeTo(200, 1);

                    $(document).on('click', o.closeButton, function(){
                        close_modal(modal_id);
                        return false;
                    })
                    $("#lean_overlay").click(function() {
                        close_modal(modal_id);
                    });

                    e.preventDefault()
                })
            });
            function close_modal(modal_id) {
                $("#lean_overlay").fadeOut(200);
                $(modal_id).css({
                    "display": "none"
                });
            }
        }
    })
})(jQuery);